package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.CodeUtils;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.OrderMedicineEntity;
import io.renren.modules.hos.entity.ShopCarEntity;
import io.renren.modules.hos.service.MedicineService;
import io.renren.modules.hos.service.OrderMedicineService;
import io.renren.modules.hos.service.ShopCarService;
import io.renren.modules.hos.utils.PageParamsUtils;
import io.renren.modules.hos.vo.OrderMedicineVo;
import io.renren.modules.hos.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.OrderDao;
import io.renren.modules.hos.entity.OrderEntity;
import io.renren.modules.hos.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
    @Autowired
    private OrderMedicineService orderMedicineService;
    @Autowired
    private MedicineService medicineService;
    @Autowired
    private ShopCarService shopCarService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryOrder(Long hosUserId, Map<String, Object> params) {
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(OrderEntity::getId);
        List<OrderEntity> orderEntities = baseMapper.selectList(queryWrapper);
        List<OrderVo> orderVos = copyList(orderEntities);
        int pageSize = PageParamsUtils.getSize(params);
        int pageIndex = PageParamsUtils.getIndex(params);
        return new PageUtils(orderVos, orderVos.size(), pageSize, pageIndex);
    }

    @Override
    public String createOrder() {
        OrderEntity orderEntity = new OrderEntity();
        String orderNum = CodeUtils.getOrderCode();
        orderEntity.setOrderNum(orderNum);
        orderEntity.setUserId(ShiroUtils.getHosUserId());
        orderEntity.setPayPrice(shopCarService.getPrice(ShiroUtils.getHosUserId()));
        orderEntity.setPayStatus(0);
        orderEntity.setCreatTime(new Date());

        this.save(orderEntity);

        return orderNum;
    }

    @Override
    public OrderEntity selectByOrderNum(String orderNum) {
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderEntity::getOrderNum, orderNum);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public OrderVo getOrderVo(OrderEntity orderEntity) {
        return copy(orderEntity);
    }


    public OrderVo copy(OrderEntity orderEntity){
        OrderVo orderVo = new OrderVo();
        orderVo.setOrderNum(orderEntity.getOrderNum());
        orderVo.setPayPrice(orderEntity.getPayPrice());
        orderVo.setPayTime(orderEntity.getPayTime());
        orderVo.setPayStatus(orderEntity.getPayStatus());
        orderVo.setOrderMedicineVos(getMedicineVo(orderEntity.getId()));

        return orderVo;
    }

    public List<OrderMedicineVo> getMedicineVo(Long orderId){
        return orderMedicineService.queryByOrderNum(orderId);
    }

    public List<OrderVo> copyList(List<OrderEntity> orderEntities){
        List<OrderVo> orderVos = new ArrayList<>();
        for (OrderEntity orderEntity : orderEntities) {
            orderVos.add(copy(orderEntity));
        }

        return orderVos;
    }


}